﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Adams.Shared
{
    public static class SecurityHelper
    {
        public static bool IsLocked(int failedLoginAttempts, DateTime lastLoginAttempt, int allowedLoginAttempts, int lockoutIncrementInMinutes)
        {
            double waited = Shared.DateHelper.DiffMins(lastLoginAttempt, DateTime.UtcNow);
            double waitfor = (failedLoginAttempts - (allowedLoginAttempts - 1)) * lockoutIncrementInMinutes;

            if (failedLoginAttempts < allowedLoginAttempts)
            {
                return false;
            }
            else
            {
                if (waited >= waitfor)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public static double GetLockWaitMinutes(int failedLoginAttempts, DateTime lastLoginAttempt, int allowedLoginAttempts, int lockoutIncrementInMinutes)
        {
            double waited = Shared.DateHelper.DiffMins(lastLoginAttempt, DateTime.UtcNow);
            double waitfor = (failedLoginAttempts - (allowedLoginAttempts - 1)) * lockoutIncrementInMinutes;

            if (waited >= waitfor)
            {
                return 0;
            }
            else
            {
                return waitfor - waited;
            }
        }
    }
}